package com.longxi.tianjintianbao.ui.presenter.houseinspection;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.houseinspection.HouseAssets;
import com.longxi.tianjintianbao.model.houseinspection.HouseCompensate;
import com.longxi.tianjintianbao.model.houseinspection.HouseEnergyMeter;
import com.longxi.tianjintianbao.model.houseinspection.HouseFacilities;
import com.longxi.tianjintianbao.model.houseinspection.YanFang;
import com.longxi.tianjintianbao.model.meter.Meter;
import com.longxi.tianjintianbao.ui.activity.houseinspection.AssetsCheckActivity;
import com.longxi.tianjintianbao.ui.activity.houseinspection.AssetsRepairActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.LabelNoBackgroundViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.houseinspection.HouseAssetsBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.houseinspection.HouseCompensateBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.houseinspection.HouseEnergyMeterBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.houseinspection.HouseFacilitiesBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.houseinspection.ICheckRoomAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.widget.AssetStatusDialog;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/9/20
 */
public class CheckRoomAtPresenter extends BasePresenter<ICheckRoomAtView> {
    private MultiTypeAdapter adapter;
    private Items items;
    private String title;
    private String type;
    private YanFang mYanFang;
    private boolean isBrokenEquipment = false;//判断是否需要生成资产赔偿单
    public boolean isFinish = false;//判断是否已完成

    public CheckRoomAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initView() {
        title = mContext.getIntent().getStringExtra(AppConst.TITLE);
        type = mContext.getIntent().getStringExtra(AppConst.TYPE);

        mContext.setTitle(title);
        mContext.setTvLeft("返回");
        initAdapter();
    }

    public void initData(YanFang yanFang) {
        isBrokenEquipment = false;
        this.mYanFang = yanFang;
        items.clear();

        if(yanFang.getAssets().size() > 0){
            items.add("房间资产类");
            items.addAll(yanFang.getAssets());

            for (HouseAssets houseAssets : yanFang.getAssets()) {
                if(houseAssets.getState().equals("破损")){
                    isBrokenEquipment = true;
                }
            }
        }

        if(yanFang.getFacilities().size() > 0){
            items.add("其它配套设施");
            items.addAll(yanFang.getFacilities());

            for (HouseFacilities houseFacilities : yanFang.getFacilities()) {
                if(houseFacilities.getState().equals("破损")){
                    isBrokenEquipment = true;
                }
            }
        }

        if(yanFang.getMeter().size() > 0){
            items.add("配套能源表");
            items.addAll(yanFang.getMeter());
        }

        showBtnUi();

        adapter.notifyDataSetChanged();
    }

    public void getCompensate(){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getCompensate(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<HouseCompensate>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<HouseCompensate> result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }

                if(result.getData() == null){
                    return;
                }

                if(result.getData().size() == 0) {
                    return;
                }

                items.clear();
                items.add("资产赔偿");
                items.addAll(result.getData());
                adapter.notifyDataSetChanged();

            }
        });
    }

    public void getCheckIn(){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getCheckIn(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<YanFang>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<YanFang> result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }

                initData(result.getData().get(0));
            }
        });
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(String.class, new LabelNoBackgroundViewBinder());
            adapter.register(HouseAssets.class, new HouseAssetsBinder(listener));
            adapter.register(HouseFacilities.class, new HouseFacilitiesBinder(listener));
            adapter.register(HouseEnergyMeter.class, new HouseEnergyMeterBinder(listener));
            adapter.register(HouseCompensate.class, new HouseCompensateBinder(listener));
            getView().getRvContent().setAdapter(adapter);
            items = new Items();
            adapter.setItems(items);
        }
    }

    private AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            Object item = items.get(position);
            if(item instanceof HouseAssets){
                HouseAssets houseAssets = (HouseAssets) item;

                switch (type) {
                    case AppConst.YAN_FANG_RUZHU:
                        Intent intent = new Intent(mContext, AssetsCheckActivity.class);
                        intent.putExtra(AppConst.BILLID, houseAssets.getBillid());
                        intent.putExtra(AppConst.CHECKER, mYanFang.getChecker());
                        intent.putExtra(AppConst.CHECKDEPT, mYanFang.getCheckdept());
                        intent.putExtra(AppConst.CHECKDATE, mYanFang.getCheckdate());
                        intent.putExtra(AppConst.NAME, houseAssets.getName());
                        intent.putExtra(AppConst.TYPE, "0");
                        intent.putExtra(AppConst.CODE, houseAssets.getCode());
                        intent.putExtra(AppConst.STATE, houseAssets.getState());
                        intent.putExtra(AppConst.YFLX, type);
                        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        break;
                    case AppConst.YAN_FANG_TUIZHU:
                        intent = new Intent(mContext, AssetsCheckActivity.class);
                        intent.putExtra(AppConst.BILLID, houseAssets.getBillid());
                        intent.putExtra(AppConst.CHECKER, mYanFang.getChecker());
                        intent.putExtra(AppConst.CHECKDEPT, mYanFang.getCheckdept());
                        intent.putExtra(AppConst.CHECKDATE, mYanFang.getCheckdate());
                        intent.putExtra(AppConst.NAME, houseAssets.getName());
                        intent.putExtra(AppConst.TYPE, "0");
                        intent.putExtra(AppConst.CODE, houseAssets.getCode());
                        intent.putExtra(AppConst.STATE, houseAssets.getState());
                        intent.putExtra(AppConst.YFLX, type);
                        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        break;
                    case AppConst.YAN_FANG_ZICHAN:
                        if(houseAssets.getState().equals("破损")){
                            intent = new Intent(mContext, AssetsRepairActivity.class);
                            intent.putExtra(AppConst.BILLID, houseAssets.getBillid());
                            mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        }
                        break;
                }

//                if(houseAssets.getState().equals("")){
//
//                }else if(houseAssets.getState().equals("破损")){
//                    mContext.jumpToActivity(AssetsRepairActivity.class);
//                }else{
//                    mContext.jumpToActivity(AssetsCheckDetailActivity.class);
//                }
            }else if(item instanceof HouseFacilities){
                HouseFacilities houseFacilities = (HouseFacilities) item;

                switch (type) {
                    case AppConst.YAN_FANG_RUZHU:
                        Intent intent = new Intent(mContext, AssetsCheckActivity.class);
                        intent.putExtra(AppConst.BILLID, houseFacilities.getBillid());
                        intent.putExtra(AppConst.CHECKER, mYanFang.getChecker());
                        intent.putExtra(AppConst.CHECKDEPT, mYanFang.getCheckdept());
                        intent.putExtra(AppConst.CHECKDATE, mYanFang.getCheckdate());
                        intent.putExtra(AppConst.NAME, houseFacilities.getName());
                        intent.putExtra(AppConst.TYPE, "1");
                        intent.putExtra(AppConst.CODE, houseFacilities.getCode());
                        intent.putExtra(AppConst.STATE, houseFacilities.getState());
                        intent.putExtra(AppConst.YFLX, type);
                        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        break;
                    case AppConst.YAN_FANG_TUIZHU:
                        intent = new Intent(mContext, AssetsCheckActivity.class);
                        intent.putExtra(AppConst.BILLID, houseFacilities.getBillid());
                        intent.putExtra(AppConst.CHECKER, mYanFang.getChecker());
                        intent.putExtra(AppConst.CHECKDEPT, mYanFang.getCheckdept());
                        intent.putExtra(AppConst.CHECKDATE, mYanFang.getCheckdate());
                        intent.putExtra(AppConst.NAME, houseFacilities.getName());
                        intent.putExtra(AppConst.TYPE, "1");
                        intent.putExtra(AppConst.CODE, houseFacilities.getCode());
                        intent.putExtra(AppConst.STATE, houseFacilities.getState());
                        intent.putExtra(AppConst.YFLX, type);
                        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        break;
                    case AppConst.YAN_FANG_ZICHAN:
                        if(houseFacilities.getState().equals("破损")){
                            intent = new Intent(mContext, AssetsRepairActivity.class);
                            intent.putExtra(AppConst.BILLID, houseFacilities.getBillid());
                            mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK);
                        }
                        break;
                }

//                if(houseFacilities.getState().equals("")){
//                }else if(houseFacilities.getState().equals("破损")){
//                    mContext.jumpToActivity(AssetsRepairActivity.class);
//                }else{
//                    mContext.jumpToActivity(AssetsCheckDetailActivity.class);
//                }
            }else if(item instanceof HouseEnergyMeter) {
                HouseEnergyMeter houseEnergyMeter = (HouseEnergyMeter) item;
                Meter meter = new Meter();
                switch (type) {
                    case AppConst.YAN_FANG_RUZHU:
                        if(!TextUtils.isEmpty(houseEnergyMeter.getStart())){
                            meter.setLastdegrees(houseEnergyMeter.getStart());
                        }
                        meter.setMeterid(houseEnergyMeter.getBillid());
                        meter.setMetername(houseEnergyMeter.getName());
                        showEnterDialog(meter);
                        break;
                    case AppConst.YAN_FANG_TUIZHU:
                        if(!TextUtils.isEmpty(houseEnergyMeter.getStart())){
                            meter.setLastdegrees(houseEnergyMeter.getEnd());
                        }
                        meter.setMeterid(houseEnergyMeter.getBillid());
                        meter.setMetername(houseEnergyMeter.getName());
                        showEnterDialog(meter);
                        break;
                    case AppConst.YAN_FANG_ZICHAN:
                        break;
                }
            }else if(item instanceof HouseCompensate) {
                HouseCompensate houseCompensate = (HouseCompensate) item;
                Intent intent = new Intent(mContext, AssetsRepairActivity.class);
                intent.putExtra(AppConst.HOUSECOMPENSATE, houseCompensate);
                mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_ASSETS_REPAIR);
            }
        }

        @Override
        public void onDelete(int position) {

        }
    };

    /**
     * 弹出抄表框
     */
    public void showEnterDialog(Meter meter){
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_enter_meter, null);
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        final TextView mTvInstrumentNumber = view.findViewById(R.id.tvInstrumentNumber);
        final EditText mEtThisdegrees = view.findViewById(R.id.etThisdegrees);
        final TextView mTvCancle = view.findViewById(R.id.tvCancle);
        final TextView mTvSure = view.findViewById(R.id.tvSure);

        String title = "";
        if(!"".equals(meter.getMeterno())){
            title = "表号:" + meter.getMeterno();
        }
        if(!"".equals(meter.getMetername())){
            title = "表名:" + meter.getMetername();
        }

        if(!TextUtils.isEmpty(meter.getLastdegrees())){
            title = title + "\t 抄表数:" + meter.getLastdegrees();
        }

        mTvInstrumentNumber.setText(title);
        mTvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String thisdegrees = mEtThisdegrees.getText().toString().trim();
                if("".equals(thisdegrees)){
                    MyUtils.showToast("请输入本次抄表度数");
                }else{
                    switch (type) {
                        case AppConst.YAN_FANG_RUZHU:
                            checkInSave(dialog, thisdegrees, meter.getMeterid());
                            break;
                        case AppConst.YAN_FANG_TUIZHU:
                            checkOutSave(dialog, thisdegrees, meter.getMeterid());
                            break;
                    }
                }
            }
        });
    }

    /**
     * 完成检验
     */
    public void checkOver() {
        switch (type) {
            case AppConst.YAN_FANG_RUZHU:

                if(isFinish){
//                    if(isBrokenEquipment){
//                        AssetStatusDialog.Builder builder = new AssetStatusDialog.Builder(mContext);
//                        String finalTitle = title;
//                        builder.setTitle(title)
//                        .setMessage("您有损坏资产/设备,是否产生资产赔偿单?")
//                        .setNegativeButton("取消",
//                                new DialogInterface.OnClickListener() {
//                                    @Override public void onClick(DialogInterface dialog, int which) {
//                                        dialog.dismiss();
//                                    }
//                                })
//                        .setPositionButton("产生", new DialogInterface.OnClickListener() {
//                            @Override public void onClick(DialogInterface dialog, int which) {
//                                createCompensate(dialog);
//                            }
//                        })
//                        .create()
//                        .show();
//                    }
                }else{
                    checkInFinishSave();
                }

                break;
            case AppConst.YAN_FANG_TUIZHU:

                if(isFinish){
                    if(isBrokenEquipment){
                        AssetStatusDialog.Builder builder = new AssetStatusDialog.Builder(mContext);
                        String finalTitle = title;
                        builder.setTitle(title)
                        .setMessage("您有损坏资产/设备,是否产生资产赔偿单?")
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    @Override public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                        .setPositionButton("产生", new DialogInterface.OnClickListener() {
                            @Override public void onClick(DialogInterface dialog, int which) {
                                createCompensate(dialog);
                            }
                        })
                        .create()
                        .show();
                    }
                }else{
                    checkOutFinishSave();
                }

                break;
            case AppConst.YAN_FANG_ZICHAN:
                    compensateFinishSave();
                break;
        }

    }

    @Override
    public void detachView() {
        super.detachView();
        this.adapter = null;
        this.items = null;
        this.mYanFang = null;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != mContext.RESULT_OK){
            return;
        }

        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_ASSETS_CHECK:
                getCheckIn();
                break;
            case AppConst.IntentRequstCode.ACTIVITY_CHECKIN_FINISH_SAVE:
                isFinish = true;
                mContext.setResult(mContext.RESULT_OK);
                showBtnUi();
                checkOver();
                break;
            case AppConst.IntentRequstCode.ACTIVITY_ASSETS_REPAIR:
                getCompensate();
                break;
        }
    }

    public void checkInSave(AlertDialog dialog, String initialdegree, String billid){

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", billid);
            object.put("type", "2");
            object.put("state", "");
            object.put("explain", "");
            object.put("initialdegree", initialdegree);
            object.put("image1", "");
            object.put("image2", "");
            object.put("image3", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkInSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功!");
                dialog.dismiss();
                getCheckIn();
            }
        });
    }

    public void checkOutSave(AlertDialog dialog, String leavedegree, String billid){

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", billid);
            object.put("type", "2");
            object.put("state", "");
            object.put("explain", "");
            object.put("leavedegree", leavedegree);
            object.put("image1", "");
            object.put("image2", "");
            object.put("image3", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkOutSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功!");
                dialog.dismiss();
                getCheckIn();
            }
        });
    }

    /**
     * 生成资产赔偿
     * @param dialog
     */
    public void createCompensate(DialogInterface dialog){

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid",  mContext.getIntent().getStringExtra(AppConst.BILLID));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().createCompensate(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("资产赔偿生成成功!");
                isBrokenEquipment = false;
                showBtnUi();
                dialog.dismiss();
                mContext.setResult(mContext.RESULT_OK);

            }
        });
    }

    public void showBtnUi(){
        switch (type) {
            case AppConst.YAN_FANG_RUZHU:
                if(isFinish){
                    getView().getTvCheckOver().setVisibility(View.GONE);
                }else{
                    getView().getTvCheckOver().setVisibility(View.VISIBLE);
                    getView().getTvCheckOver().setText("完成验房");
                }
                break;
            case AppConst.YAN_FANG_TUIZHU:
                if(isFinish){
                    if(isBrokenEquipment){
                        getView().getTvCheckOver().setVisibility(View.VISIBLE);
                        getView().getTvCheckOver().setText("生成赔偿单");
                    }else{
                        getView().getTvCheckOver().setVisibility(View.GONE);
                    }
                }else{
                    getView().getTvCheckOver().setVisibility(View.VISIBLE);
                    getView().getTvCheckOver().setText("完成验房");
                }
                break;
            case AppConst.YAN_FANG_ZICHAN:
                if(isFinish){
                    getView().getTvCheckOver().setVisibility(View.GONE);
                }else{
                    getView().getTvCheckOver().setVisibility(View.VISIBLE);
                    getView().getTvCheckOver().setText("完成赔偿");
                }
                break;
        }
    }

    /**
     * 生成资产赔偿
     */
    public void checkInFinishSave(){

        String otherexplain = getView().getEtOtherexplain().getText().toString().trim();
        String hygieneexplain = getView().getEtHygieneexplain().getText().toString().trim();
        if(TextUtils.isEmpty(otherexplain)){
            getView().getEtOtherexplain().setFocusable(true);
            MyUtils.showToast("请输入其他说明");
            return;
        }
        if(TextUtils.isEmpty(hygieneexplain)){
            MyUtils.showToast("请输入卫生说明");
            return;
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid",  mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("otherexplain",  otherexplain);
            object.put("hygieneexplain",  hygieneexplain);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkInFinishSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("入住验房完成!");
                isFinish = true;
                mContext.setResult(mContext.RESULT_OK);
                showBtnUi();
                checkOver();
            }
        });
    }

    /**
     * 退住验房完成保存
     */
    public void checkOutFinishSave(){

        String otherexplain = getView().getEtOtherexplain().getText().toString().trim();
        String hygieneexplain = getView().getEtHygieneexplain().getText().toString().trim();
        if(TextUtils.isEmpty(otherexplain)){
            getView().getEtOtherexplain().setFocusable(true);
            MyUtils.showToast("请输入其他说明");
            return;
        }
        if(TextUtils.isEmpty(hygieneexplain)){
            MyUtils.showToast("请输入卫生说明");
            return;
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid",  mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("otherexplain",  otherexplain);
            object.put("hygieneexplain",  hygieneexplain);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkOutFinishSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("退住验房完成!");
                isFinish = true;
                mContext.setResult(mContext.RESULT_OK);
                showBtnUi();
                checkOver();
            }
        });
    }

    /**
     * 退住验房完成保存
     */
    public void compensateFinishSave(){

        String amount = getView().getEtAmount().getText().toString().trim();
        if(TextUtils.isEmpty(amount)){
            getView().getEtOtherexplain().setFocusable(true);
            MyUtils.showToast("请输入赔偿合计金额");
            return;
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid",  mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("amount",  amount);
            object.put("person",  MyApplication.loginUser.username);
            object.put("persondept",  MyApplication.loginUser.dept);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().compensateFinishSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("资产赔偿完成!");
                isFinish = true;
                mContext.setResult(mContext.RESULT_OK);
                showBtnUi();
            }
        });
    }
}
